home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / min / fsolver.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-22  |  4.6 KB  |  176 lines

  1. /* min/fsolver.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <gsl/gsl_errno.h>
  24. #include <gsl/gsl_min.h>
  25.  
  26. #include "min.h"
  27.  
  28. static int 
  29. compute_f_values (gsl_function * f, double minimum, double * f_minimum,
  30.                   double x_lower, double * f_lower, 
  31.                   double x_upper, double * f_upper);
  32.  
  33.  
  34. static int 
  35. compute_f_values (gsl_function * f, double minimum, double * f_minimum,
  36.                   double x_lower, double * f_lower,
  37.                   double x_upper, double * f_upper)
  38. {
  39.   SAFE_FUNC_CALL(f, x_lower, f_lower);
  40.   SAFE_FUNC_CALL(f, x_upper, f_upper);
  41.   SAFE_FUNC_CALL(f, minimum, f_minimum);
  42.  
  43.   return GSL_SUCCESS;
  44. }
  45.  
  46. int
  47. gsl_min_fminimizer_set (gsl_min_fminimizer * s, 
  48.                         gsl_function * f, 
  49.                         double minimum, double x_lower, double x_upper)
  50. {
  51.   int status ;
  52.  
  53.   double f_minimum, f_lower, f_upper;
  54.  
  55.   status = compute_f_values (f, minimum, &f_minimum, 
  56.                              x_lower, &f_lower,  
  57.                              x_upper, &f_upper);
  58.  
  59.   if (status != GSL_SUCCESS)
  60.     {
  61.       return status ;
  62.     }
  63.   
  64.   status = gsl_min_fminimizer_set_with_values (s, f, minimum, f_minimum, 
  65.                                                x_lower, f_lower,
  66.                                                x_upper, f_upper);
  67.   return status;
  68. }
  69.  
  70. gsl_min_fminimizer *
  71. gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * T) 
  72. {
  73.   gsl_min_fminimizer * s = 
  74.     (gsl_min_fminimizer *) malloc (sizeof (gsl_min_fminimizer));
  75.  
  76.   if (s == 0)
  77.     {
  78.       GSL_ERROR_VAL ("failed to allocate space for minimizer struct",
  79.             GSL_ENOMEM, 0);
  80.     };
  81.  
  82.   s->state = malloc (T->size);
  83.  
  84.   if (s->state == 0)
  85.     {
  86.       free (s);        /* exception in constructor, avoid memory leak */
  87.  
  88.       GSL_ERROR_VAL ("failed to allocate space for minimizer state",
  89.             GSL_ENOMEM, 0);
  90.     };
  91.  
  92.   s->type = T ;
  93.   s->function = NULL;
  94.  
  95.   return s;
  96. }
  97.  
  98. int
  99. gsl_min_fminimizer_set_with_values (gsl_min_fminimizer * s, gsl_function * f, 
  100.                                     double minimum, double f_minimum, 
  101.                                     double x_lower, double f_lower,
  102.                                     double x_upper, double f_upper)
  103. {
  104.   s->function = f;
  105.   s->minimum = minimum;
  106.   s->x_lower = x_lower;
  107.   s->x_upper = x_upper;
  108.  
  109.   if (x_lower > x_upper)
  110.     {
  111.       GSL_ERROR ("invalid interval (lower > upper)", GSL_EINVAL);
  112.     }
  113.  
  114.   if (minimum >= x_upper || minimum <= x_lower) 
  115.     {
  116.       GSL_ERROR ("minimum must lie inside interval (lower < x < upper)",
  117.                  GSL_EINVAL);
  118.     }
  119.  
  120.   s->f_lower = f_lower;
  121.   s->f_upper = f_upper;
  122.   s->f_minimum = f_minimum;
  123.  
  124.   if (f_minimum >= f_lower || f_minimum >= f_upper)
  125.     {
  126.       GSL_ERROR ("endpoints do not enclose a minimum", GSL_EINVAL);
  127.     }
  128.  
  129.   return (s->type->set) (s->state, s->function, 
  130.                          minimum, f_minimum, 
  131.                          x_lower, f_lower,
  132.                          x_upper, f_upper);
  133. }
  134.  
  135.  
  136. int
  137. gsl_min_fminimizer_iterate (gsl_min_fminimizer * s)
  138. {
  139.   return (s->type->iterate) (s->state, s->function, 
  140.                              &(s->minimum), &(s->f_minimum),
  141.                              &(s->x_lower), &(s->f_lower), 
  142.                              &(s->x_upper), &(s->f_upper));
  143. }
  144.  
  145. void
  146. gsl_min_fminimizer_free (gsl_min_fminimizer * s)
  147. {
  148.   free (s->state);
  149.   free (s);
  150. }
  151.  
  152. const char *
  153. gsl_min_fminimizer_name (const gsl_min_fminimizer * s)
  154. {
  155.   return s->type->name;
  156. }
  157.  
  158. double
  159. gsl_min_fminimizer_minimum (const gsl_min_fminimizer * s)
  160. {
  161.   return s->minimum;
  162. }
  163.  
  164. double
  165. gsl_min_fminimizer_x_lower (const gsl_min_fminimizer * s)
  166. {
  167.   return s->x_lower;
  168. }
  169.  
  170. double
  171. gsl_min_fminimizer_x_upper (const gsl_min_fminimizer * s)
  172. {
  173.   return s->x_upper;
  174. }
  175.  
  176.